package LeetCode;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}16:59
 */
public class LeetCode892 {

    public static void main(String[] args) {
        int[][] grid = {{1,1,1},{1,0,1},{1,1,1}};
        System.out.println(new LeetCode892().surfaceArea(grid));
    }
    public int surfaceArea(int[][] grid) {
        int area = 0;
        for (int i = 0;i < grid.length;i++){
            for (int j = 0; j <grid[i].length;j++){
                int height = grid[i][j];
                if (height > 0) {
                    //没有侧面遮挡时的面积
                    area += (height * 4) + 2;
                    //计算i坐标方向的遮挡面积
                    area -= i > 0 ? Math.min(height, grid[i - 1][j]) * 2 : 0;
                    //计算j坐标方向的遮挡面积
                    area -= j > 0 ? Math.min(height, grid[i][j - 1]) * 2 : 0;
                }
            }
        }
        return area;
    }
}
